package com.tbs.parkingsaleapp.UI.LoginOrSign.Base;

import abstergo.tbs.betterasyc.AsycBase.AsycWorker;
import abstergo.tbs.betterasyc.AsycBase.IAsycResultAction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tbs.parkingsaleapp.Http.Actionner.ADResulter;
import com.tbs.parkingsaleapp.Http.Actionner.FaceLoginner;
import com.tbs.parkingsaleapp.Http.JsonData.AdResult;
import com.tbs.parkingsaleapp.Http.JsonData.FaceCheckResult;
import com.tbs.parkingsaleapp.Http.JsonData.LoginResult;
import com.tbs.parkingsaleapp.Http.Listenner.ADHttper;
import com.tbs.parkingsaleapp.Http.Listenner.FaceLoginHttper;
import com.tbs.parkingsaleapp.SystemManager.LoadToastMananger;
import com.tbs.parkingsaleapp.UI.Listenners.FaceLoginClicker;
import com.tbs.parkingsaleapp.R;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

public class FaceLoginFragment extends Fragment implements IAsycResultAction<FaceCheckResult>,  OnBannerListener {

    ADResulter ader;
    public boolean LoginReady()
    {
        return id.getText().toString().length()>0;
    }
    public String getID()
    {
        return id.getText().toString();
    }
    Banner banner;
    EditText id;

    TextView comfirm;
    public FaceLoginFragment() {

    }
    Switch isrem;
    public boolean IsRemember()
    {
        return isrem.isChecked();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
      View main=  inflater.inflate(R.layout.fragment_face_login, container, false);
      comfirm=main.findViewById(R.id.comfirm);
      comfirm.setOnClickListener(new FaceLoginClicker(this));
      id=main.findViewById(R.id.faceid);
      isrem=main.findViewById(R.id.remlogin);
      banner=main.findViewById(R.id.adbanner);
        banner.setOnBannerListener(this);
        AsycWorker<String,AdResult[]> ad=new AsycWorker<>(1);
        ader=new ADResulter(banner);
        ad.setResultAction(ader).setContext(getContext());
        ad.setBackgroundAction(new ADHttper());
        ad.Run();
      return main;
    }

    @Override
    public void OnBannerClick(int position) {
        if(ader.getADS()!=null&&ader.getADS().length>position)
        {
            Uri uri = Uri.parse(ader.getADS()[position].getUrl());    //设置跳转的网站
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }
    @Override
    public void ResultFunction(FaceCheckResult result) {
        if(result.getResultCode()==1)
        {
            LoadToastMananger.Show(getContext(),"正在进行验证您的身份证...",800);
            AsycWorker<String, LoginResult> login=new AsycWorker<>(1);
            login.setBackgroundAction(new FaceLoginHttper(this)).setContext(getContext());
            login.setResultAction(new FaceLoginner(this));
            login.Run(getID(),result.getToken().getToken());
        }
        else
        {
            LoadToastMananger.Hide();
            Toast.makeText(getContext(),"人脸验证失败",Toast.LENGTH_SHORT).show();
        }
    }
}